//
// Created by 吴喆 on 2020/3/5.
//

#include "Scene.h"
#include "CollisionListener.h"
#include "Player.h"

Scene::Scene() {}

Scene::~Scene() {
    for(auto gameObject : gameObjects) {
        delete gameObject;
    }
    delete camera;
    delete map;
    delete collisionListener;
}

void Scene::update() {
    // box2d进一步
    world->step();
    map->update();
    // 遍历gameObject并更新逻辑
    for (auto gameObject : gameObjects) {
        gameObject->update();
    }
    player->update();
    camera->update();
}

void Scene::render() {
    currentFrame = backgroundAnimation.updateAndGetCurrentFrame();
    renderBackground(*backgroundRenderer);
    map->render();
    for (auto gameObject : gameObjects) {
        gameObject->render();
    }
    player->render();
}

void Scene::init() {
    initBackground();
    initMap();
    initPlayer();
    initCamera();
    initRenderer();
}

void Scene::initMap() {
    world = new World(50.0f);
    collisionListener = new CollisionListener();
    world->b2world->SetContactListener(collisionListener);
}

void Scene::initCamera() {
    Scene::camera = new Camera(player->speed);
    camera->initTrackObject(player);
}

Camera *Scene::getCamera() const {
    return camera;
}



